package com.tdx.blog_nowcoder.controller;

import com.tdx.blog_nowcoder.entity.MyEvent;
import com.tdx.blog_nowcoder.entity.User;
import com.tdx.blog_nowcoder.utils.event.EventProducer;
import com.tdx.blog_nowcoder.service.FollowService;
import com.tdx.blog_nowcoder.service.UserService;
import com.tdx.blog_nowcoder.utils.DumpJSONToString;
import com.tdx.blog_nowcoder.utils.Page;
import com.tdx.blog_nowcoder.utils.ThreadHostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

import static com.tdx.blog_nowcoder.constant.CommentReplyConstant.ENTITY_TYPE_USER;
import static com.tdx.blog_nowcoder.constant.KafkaTopicConstant.TOPIC_FOLLOW;

/**
* @description: TODO
* @author tdx
* @date 2022/7/3 16:51
* @version 1.0
*/
@Controller
public class FollowController {

    @Autowired
    private FollowService followService;


    @Autowired
    private ThreadHostHolder threadHostHolder;

    @Autowired
    private UserService userService;

    @Autowired
    private EventProducer eventProducer;

    @RequestMapping(path = "/follow",method = RequestMethod.POST)
    @ResponseBody
    public String follow(int entityType,int entityId){
        User user = threadHostHolder.getUser();
        followService.follow(user.getId(), entityType, entityId);

        //触发关注事件
        MyEvent event = new MyEvent()
                .setTopic(TOPIC_FOLLOW)
                .setUserId(threadHostHolder.getUser().getId())
                .setEntityType(entityType)
                .setEntityId(entityId)
                .setEntityUserId(entityId); // 关注了谁


        eventProducer.fireEvent(event);

        return DumpJSONToString.getJsonString(200, "已关注!");

    }

    @RequestMapping(path = "/unfollow",method = RequestMethod.POST)
    @ResponseBody
    public String unfollow(int entityType,int entityId){
        User user = threadHostHolder.getUser();
        followService.unfollow(user.getId(), entityType, entityId);
        return DumpJSONToString.getJsonString(200, "已取消关注!");
    }

    @RequestMapping(path = "/followers/{userId}",method = RequestMethod.GET)
    public String getFollowers(@PathVariable("userId") int userId, Page page, Model model){
        User user = userService.selectUserById(userId);
        if(user==null){
            throw new RuntimeException("该用户不存在!");
        }
        model.addAttribute("user",user);
        //设置分页的信息
        page.setPerPageSize(5);
        page.setTotalPageNums((int) followService.getFollowerCount(ENTITY_TYPE_USER,userId));
        page.setPath("/followers/"+userId);


        List<Map<String, Object>> userList = followService.findFollowers(userId, page.getOffset(), page.getPerPageSize());
        if (userList != null) {
            for (Map<String, Object> map : userList) {
                User u = (User) map.get("user");
                map.put("hasFollowed", hasFollowed(u.getId()));
            }
        }
        model.addAttribute("users", userList);

        return "/site/follower";



    }
    private boolean hasFollowed(int userId) {
        if (threadHostHolder.getUser() == null) {
            return false;
        }
        return followService.hasFollowed(threadHostHolder.getUser().getId(), ENTITY_TYPE_USER, userId);
    }

    @RequestMapping(path = "/followees/{userId}",method = RequestMethod.GET)
    public String getFollowees(@PathVariable("userId") int userId, Page page, Model model){
        User user = userService.selectUserById(userId);
        if(user==null){
            throw new RuntimeException("该用户不存在!");
        }
        model.addAttribute("user",user);
        //设置分页的信息
        page.setPerPageSize(5);
        page.setTotalPageNums((int) followService.getFolloweeCount(userId,ENTITY_TYPE_USER));
        page.setPath("/followees/"+userId);


        List<Map<String, Object>> userList = followService.findFollowees(userId, page.getOffset(), page.getPerPageSize());
        if (userList != null) {
            for (Map<String, Object> map : userList) {
                User u = (User) map.get("user");
                map.put("hasFollowed", hasFollowed(u.getId()));
            }
        }
        model.addAttribute("users", userList);
        return "/site/followee";
    }






}
